<?php
	/**
	 * Created by 绵阳市碳素云信息技术有限责任公司.
	 * User: castle
	 * Date: 2015/06/23
	 * Time: 11:22
	 */
	include 'Lib/function.php';
	include 'Lib/Tsy.class.php';
	class SocketServer{
		public $redis ;
		public $serv ;
		public $app ;
		public function __construct($ip,$port){

			if(file_exists('init.php')){
				include 'init.php';
			}

			$this->serv = new swoole_server($ip, $port);

			$this->serv->set([
//				'heartbeat_check_interval' => 5, //每隔多少秒检测一次，单位秒，Swoole会轮询所有TCP连接，将超过心跳时间的连接关闭掉
//				'heartbeat_idle_time' => 120, //TCP连接的最大闲置时间，单位s , 如果某fd最后一次发包距离现在的时间超过
				//				'daemonize' => 1, //自动进入守护进程
				//				'log_file' => 'swoole.log'
			]);

// attach handler for connect event, once client connected to server the registered handler will be executed
			$this->serv->on('connect', function ($serv, $fd){
				include 'onConnect.php';
			});
// attach handler for receive event, every piece of data received by server, the registered handler will be
// executed. And all custom protocol implementation should be located there.
			$this->serv->on('receive', function ($serv, $fd, $from_id, $data) {
				//TODO 实现session机制
				$data = json_decode($data,true);
				include 'onReceive.php';
			});

			$this->serv->on('close', function ($serv, $fd) {
				include 'onClose.php';
			});

			ob_start();

			define("APP_PATH",  realpath(dirname(__FILE__) . '/../')); /* 指向public的上一级 */
			$GLOBALS['Yaf'] = new Yaf_Application(APP_PATH . "/conf/application.ini");
			$GLOBALS['Yaf']->bootstrap()->run();

			ob_end_clean();

			// 系统信息
			if(version_compare(PHP_VERSION,'5.4.0','<')) {
				ini_set('magic_quotes_runtime',0);
				define('MAGIC_QUOTES_GPC',get_magic_quotes_gpc()? true : false);
			}else{
				define('MAGIC_QUOTES_GPC',false);
			}

			$this->serv->start();

		}
	}
	$S = new SocketServer('0.0.0.0',46031);